package com.example.aircraftticketingsystemserver.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.aircraftticketingsystemserver.entity.Order;
import com.example.aircraftticketingsystemserver.entity.OrderDTO;
import com.example.aircraftticketingsystemserver.mapper.OrdersMapper;
import com.example.aircraftticketingsystemserver.service.IOrdersService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.aircraftticketingsystemserver.utils.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lxz
 * @since 2024-08-05
 */
@Service
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Order> implements IOrdersService {
    @Resource
    OrdersMapper ordersMapper;
    @Override
    public void addOrder(Order order) {
        ordersMapper.addOrder(order);
    }

    @Override
    public Page getOrderDetailByUserId(Integer pageNo,
                                       Integer pageSize,
                                       Integer userId) {
        Page page =new Page();
        PageHelper.startPage(pageNo,pageSize);
        List<OrderDTO> orderDTOS=ordersMapper.findOrderDetailsByUserId(userId);
        PageInfo<OrderDTO> info=new PageInfo<>(orderDTOS);
        page.setPage(info.getList());
        page.setPageNo(info.getPageNum());
        page.setPageSize(info.getPageSize());
        page.setHasNext(info.isHasNextPage());
        page.setHasPre(info.isHasPreviousPage());
        page.setTotal(info.getTotal());
        page.setPageCount(info.getPages());
        return page;
    }

    @Override
    public void updateOrder(Order order) {
        ordersMapper.updateOrder(order);
    }

    @Override
    public List<Order> getOrderByFlightId(Integer flightId) {
        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("`flight_id`", flightId);
        return baseMapper.selectList(queryWrapper);
    }
}
